<template>
  <div>
    <div style="margin-bottom: 20px;">
      <el-collapse accordion>
        <el-collapse-item>
          <template slot="title">
            <el-row type="flex" align="middle" style="width: 100%">
              <el-col :span="2" class="item-title">活动类型</el-col>
              <el-col :span="20">
                {{activityInfo.ptype=='2'?'打折':activityInfo.ptype=='4'?'特价':''}}({{activityInfo.isLimited ?'限购'+activityInfo.limitNum+'份':'不限购'}})
              </el-col>
              <el-col :span="2" style='text-align: right;color: #999; margin-right: 4px'>展开</el-col>
            </el-row>
          </template>
          <el-row type="flex" align="middle" class="item-item">
            <el-col :span="2" class="item-title">商家</el-col>
            <el-col :span="22">{{activityInfo.busname}}</el-col>
          </el-row>
          <el-row type="flex" align="middle" class="item-item">
            <el-col :span="2" class="item-title">有效日期</el-col>
            <el-col :span="22">{{activityInfo.starttime}} - {{activityInfo.endtime}}</el-col>
          </el-row>
          <el-row type="flex" align="middle" class="item-item">
            <el-col :span="2" class="item-title">有效星期</el-col>
            <el-col :span="22">{{activityInfo.worktoday|filtration}}</el-col>
          </el-row>
          <el-row type="flex" align="middle" class="item-item">
            <el-col :span="2" class="item-title">有效时间</el-col>
            <el-col :span="22">{{activityInfo.firstTime|filtration1(activityInfo.secondTime,activityInfo.thirdTime)}}
            </el-col>
          </el-row>
          <el-row type="flex" align="middle" class="item-item">
            <el-col :span="2" class="item-title">是否同享</el-col>
            <el-col :span="22">{{activityInfo.shared?'同享':'不同享'}}</el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>
    </div>
    <el-row class="table-row" style="margin-top: 20px">
      <el-row type="flex" align="middle" justify="space-between" class="search">
        <el-col :span="4" style="font-weight: 600;"> 可选商品</el-col>
        <el-col :span="6">
          <el-input placeholder="请输入搜索关键字" v-model="name" class="input-with-select" size="small">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-row type="flex" class="act-com">
        <el-col :span="2" class="menu-list">
          <ul>
            <li class=" text-over" v-for="(list,index) in tableData.list" :key="list.id"
                @click="switchClassification(index)" :class="{'cur':actIndex ==index}">{{list.name}}
            </li>
          </ul>
        </el-col>
        <el-col :span="22">
          <div class="base-table-wrap">
            <el-table :data="actIndexData" stripe style="width: 100%" >
              <el-table-column type="index" label="#" width="40" align="letf"></el-table-column>
              <el-table-column prop="name" label="商品名称" min-width="120" align="letf"></el-table-column>
              <el-table-column prop="status" label="商品状态" min-width="100" align="center">
                <template slot-scope="scope">
                  {{scope.row.status =='0'?'正常销售':'暂停销售'}}
                </template>
              </el-table-column>
              <el-table-column label="规格" min-width="100" align="center">
                <template slot-scope="scope">
                  <el-row class="act-set" type="felx" justify="space-between"
                          v-for="item in scope.row.standardList" :key="item.id">
                    {{item.name}}
                  </el-row>
                </template>
              </el-table-column>
              <el-table-column label="价格" min-width="100" align="center">
                <template slot-scope="scope">
                  <el-row class="act-set" type="felx" justify="space-between"
                    v-for="item in scope.row.standardList" :key="item.id">
                    {{comConfig.mark}}{{item.price}}
                  </el-row>
                </template>
              </el-table-column>
              <el-table-column label="活动信息" min-width="100" align="center">
                <template slot-scope="scope">
                  <div class="act-set" v-for="item in scope.row.standardList" :key="item.id">
                    <span v-if="item.activityTarget.discount > '0' || item.activityTarget.disprice > '0'" class="act-wrod">
                      {{item.activityTarget.discount|cationPirce(item.activityTarget.disprice)}}
                    </span>
                    <span v-else class="">未参与</span>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="操作" min-width="100" align="right">
                <template slot-scope="scope">
                  <div class="act-set" v-for="item in scope.row.standardList" :key="item.id">
                    <span v-if="activityInfo.ptype =='2' && item.activityTarget.discount > '0' 
                      && item.activityTarget.discount && (item.activityTarget.activityid == activityInfo.id)">
                      <el-button @click="huifuAct(item)" type="primary" plain style="padding: 2px" size="mini">恢复</el-button>
                    </span>
                    <span v-if="activityInfo.ptype =='4'&& item.activityTarget.disprice >'0' && !item.activityTarget.discount
                     && (item.activityTarget.activityid == activityInfo.id)">
                      <el-button @click="huifuAct(item)" type="primary" plain style="padding: 2px" size="mini">恢复</el-button>
                    </span>
                    <el-button @click="toAmend(item,scope.row.name)" type="danger" style="padding: 2px" size="mini"
                      :disabled="(activityInfo.ptype =='2' && !item.activityTarget.discount && item.activityTarget.disprice > '0')
                      || (activityInfo.ptype =='4' && item.activityTarget.discount > '0' && item.activityTarget.disprice > '0')">
                      编辑
                    </el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
            <!-- 复制 el-table v-loading  v-loading无法快速响应 所有自定义 -->
            <div class="base-directives-loading" v-show="isloadingTable">
              <div class="el-loading-spinner">
                <svg viewBox="25 25 50 50" class="circular">
                  <circle cx="50" cy="50" r="20" fill="none" class="path"></circle>
                </svg>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </el-row>
    <el-dialog title="选商品图片" v-dialogDrag :visible.sync="dialogTableVisible" width="600px" :close-on-press-escap="true">
      <el-form :model="$data" ref="ruleForm" label-width="100px"  size="small">
        <el-form-item label="商品信息">
          <el-row>
            <span>{{names}}({{theNameOfTheSpecification}})</span> <span>,当前价格:{{price}}{{comConfig.unit}}</span>
          </el-row>
        </el-form-item>
        <el-form-item v-if="activityInfo.ptype =='2'" prop="newDiscount" label="折扣比例" :rules="[{required: true, message: '折扣比例未填'}]">
          <el-input-number style="margin-right: 10px" v-model="newDiscount" :min='1' :max='99' :precision='0' /> %
        </el-form-item>
        <el-form-item v-if="activityInfo.ptype =='2'" prop="newDiscount" label="折后价格">{{getDisprice()}}{{comConfig.unit}}</el-form-item>
        <el-form-item v-if="activityInfo.ptype =='4'" prop="newDisprice" label="特价" :rules="[{required: true, message: '特价金额未填'}]">
          <el-input-number style="margin-right: 10px" v-model="newDisprice" :min='0' :max='price' :precision='2' />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="issubmitLoading" @click="submitForm('ruleForm')">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import acction from '@/utils/acction.js'
  export default {
    data() {
      return {
        dialogTableVisible: false, // 编辑折扣或者特价
        isloadingTable: false, // 表单等待
        issubmitLoading: false, // 提交修改折扣或者特价
        activityInfo: {}, // 活动信息
        name: '', // 搜索商品关键字
        actIndex: '0', // 当前菜单
        tableData: {},
        actIndexData: [], // 当前显示的商品
        //编辑数据
        id: '',
        names: '',
        discount:'',
        disprice:'',
        newDiscount:undefined,
        newDisprice:undefined,
        price: '',
        theNameOfTheSpecification: '',
      }
    },
    filters: {
      cationPirce(discount,disprice){
        if (disprice > '0') {
          if (discount > '0') {
            return `${new acction(discount,10).accMul()}折，${disprice + comConfig.unit}`
          } else {
            return `特价${disprice + comConfig.unit}`
          }
        } else {
          return '未参与活动'
        }
        //return new acction(str,num).accMul()
      },
      filtration(dd) {
        let str = ''
        if (dd) {
          let arr = []
          dd.forEach((item, index) => {
            if (item == '1') arr.push('周一')
            if (item == '2') arr.push('周二')
            if (item == '3') arr.push('周三')
            if (item == '4') arr.push('周四')
            if (item == '5') arr.push('周五')
            if (item == '6') arr.push('周六')
            if (item == '7') arr.push('周日')
          })
          str += arr.join('、')
        }
        return str

      },
      filtration1(t2, t3, t4) {
        let str = ''
        if (t2) {
          str += t2.split(',').join('-')
          if (t3) {
            str += '、' + t3.split(',').join('-')
          }
          if (t4) {
            str += '、' + t4.split(',').join('-')
          }
        } else {
          str += '全天'
        }
        return str
      },
    },
    methods: {
      getDisprice() {
        let discount = this.newDiscount ? this.newDiscount : '0'
        let discounts = new acction(discount,100).accDiv()
        let price = this.price ? this.price : '0'
        let a = new acction(discounts,price).accMul()
        return a > 0.01 ? a : 0.01
      },
      toAmend(row, name) {  //开始编辑
        this.names = name
        this.id = row.id
        this.price = row.price
        this.disprice = row.activityTarget.disprice||undefined
        this.discount = row.activityTarget.discount?new acction(row.activityTarget.discount,100).accMul():undefined
        this.newDiscount =this.discount
        this.newDisprice = this.disprice
        this.theNameOfTheSpecification = row.name
        this.dialogTableVisible = true
      },
      switchClassification(index) {//切换分类
        this.actIndexData = this.tableData.list[index].goodsSells
        this.actIndex = index
      },
      getTableList() {  //获取商品数据
        this.isloadingTable = true
        const params = {name: this.name, activityId: this.$route.query.id}
        this.$http.get('/server/business/setActivityToGoodssellStandard?' + this.qs.stringify(params)).then(res => {
          if (res.data.success) {
            this.tableData = res.data.data
            let length = this.tableData.list.length
            if (length > 0) {
              if (length >= this.actIndex) {
                this.actIndexData = this.tableData.list[this.actIndex].goodsSells
              } else {
                this.actIndexData = this.tableData.list[0].goodsSells
              }
            }
            if (this.tableData.list.length > 0) {
              this.className = this.tableData.list.length > 0 ? this.tableData.list[0].name : ''
              this.classId = this.tableData.list.length > 0 ? this.tableData.list[0].id : null
            }
          }
        }).finally(() => { this.isloadingTable = false })
      },
      huifuAct(row,list){  //回复活动
        const params ={ standarid:row.id,activityid:this.$route.query.id }
        this.isloadingTable = true
        this.$http.get('/server/activity/deleteStandardActivity?' + this.qs.stringify(params)).then(res => {
          if (res.data.success) {
            this.getTableList()
          }
        }).finally(() => { this.isloadingTable = false })
      },
      getActivityInfo() { //获取活动信息
        this.isloadingTable = true
        this.$http.get('/server/activity/info?activityId=' + this.$route.query.id, {headers: {isCancel: true}}).then(res => {
          if ( res.data.success) {
            this.activityInfo = res.data.data
            this.getTableList()
          }
        }).finally(() => { this.isloadingTable = false })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const params = {standarid: this.id, activityid: this.$route.query.id}
            if (this.activityInfo.ptype == '2') {
              params.discount =new acction(this.newDiscount,100).accDiv()
              params.disprice = this.getDisprice()
            } else if (this.activityInfo.ptype == '4') {
              params.disprice = this.newDisprice
            }
            this.issubmitLoading = true
            this.$http.post('/server/activity/saveStandardActivity', JSON.stringify(params)).then(res => {
              if (res.data.success) {
                this.$message({type: 'success', message: '设置商品活动成功'})
                this.dialogTableVisible = false
                this.getTableList()
              }
            }).finally(() => { this.issubmitLoading = false })
          } else {
            this.$message.error('有不合法的输入或未填写的输入，请检查页面修改再提交')
            return false
          }
        })
      }
    },
    mounted() {
      if (this.$route.query.id) {
        this.getActivityInfo()
      } else {
        this.$message({type: 'error', message: '当前页面信息有误!'})
        this.$router.push({path: '/main/activity/pron'})
      }
    }
  }
</script>
<style lang="scss" scoped>
  @import './css/citem.scss';
</style>
